package com.xxg.renrenlogin.controller;

import com.auth0.jwt.exceptions.JWTCreationException;
import com.xxg.renrenlogin.domain.query.*;
import com.xxg.renrenlogin.domain.result.BaseResult;
import com.xxg.renrenlogin.domain.result.LoginReturnCodeEnum;
import com.xxg.renrenlogin.domain.result.TokenResult;
import com.xxg.renrenlogin.exception.ErrorMessage;
import com.xxg.renrenlogin.service.LoginService;
import com.xxg.renrenlogin.service.WeChatService;
import com.xxg.renrenlogin.util.DateTimeUtil;
import com.xxg.renrenlogin.util.JwtUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 登录认证接口
 *
 * @author xxg
 * @date 2021-11-15
 */
@Api(tags = "登录认证接口")
@RestController
@RequestMapping("/")
@CrossOrigin
public class LoginController {

    private final LoginService loginService;
    private final WeChatService weChatService;

    public LoginController(LoginService loginService, WeChatService weChatService) {
        this.loginService = loginService;
        this.weChatService = weChatService;
    }

    @ApiOperation("Token令牌登录")
    @RequestMapping(value = "tokenLogin", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE,
            consumes = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({@ApiImplicitParam(name = "tokenLoginQuery", value = "令牌", required = true, dataType = "TokenLoginQuery")})
    public BaseResult tokenLogin(@RequestBody @Validated TokenLoginQuery tokenLoginQuery) {
        return loginService.tokenLogin(tokenLoginQuery);
    }

    @ApiOperation("获取验证码")
    @RequestMapping(value = "getCode", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE,
            consumes = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({@ApiImplicitParam(name = "mobileInfoQuery", value = "手机号码", required = true, dataType = "MobileInfoQuery")})
    public BaseResult getCode(@RequestBody @Validated MobileInfoQuery mobileInfoQuery) {
        return loginService.getCode(mobileInfoQuery.getMobile());
    }

    @ApiOperation("验证码登录")
    @RequestMapping(value = "codeLogin", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE,
            consumes = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({@ApiImplicitParam(name = "codeLoginQuery", value = "验证码", required = true, dataType = "CodeLoginQuery")})
    public BaseResult codeLogin(@RequestBody @Validated CodeLoginQuery codeLoginQuery) {
        return loginService.codeLogin(codeLoginQuery);
    }

//    @ApiOperation("获取测试token")
//    @RequestMapping(value = "getTestToken", method = RequestMethod.GET)
//    public BaseResult getTestToken() {
//        String account = "13372275566";
//        String loginToken;
//        try {
//            loginToken = JwtUtils.createJwtToken(account, DateTimeUtil.getTimestampString(), "8", "12", JwtUtils.SECRET);
//        } catch (JWTCreationException e) {
//            return new BaseResult(LoginReturnCodeEnum.FAILURE, ErrorMessage.CREATE_TOKEN_ERROR, null);
//        }
//        TokenResult tokenInfo;
//        tokenInfo = new TokenResult(loginToken, "3");
//        return new BaseResult(LoginReturnCodeEnum.SUCCESS, "", tokenInfo);
//    }

    @ApiOperation("用户名和密码登录")
    @RequestMapping(value = "passwordLogin", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE,
            consumes = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({@ApiImplicitParam(name = "passwordLoginQuery", value = "用户名和密码", required = true, dataType = "PasswordLoginQuery")})
    public BaseResult passwordLogin(@RequestBody @Validated PasswordLoginQuery passwordLoginQuery) {
        return loginService.passwordLogin(passwordLoginQuery.getUserName(), passwordLoginQuery.getPassword());
    }

    @ApiOperation("微信小程序认证")
    @RequestMapping(value = "weChatLPVerify", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE,
            consumes = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({@ApiImplicitParam(name = "weChatCodeQuery", value = "微信code", required = true, dataType = "WeChatCodeQuery")})
    public BaseResult weChatLPVerify(@RequestBody @Validated WeChatCodeQuery weChatCodeQuery) {
        return weChatService.lpVerify(weChatCodeQuery.getCode());
    }
}
